In this post I introduce the R package “here” which enables you to get rid of the common approach setwd(). “Here” uses the top-level directory for the file you are working on and does not depend on your file organization. Moreover, I will show you some tricks on plotting your data with ggplot, especially laying out plots in different grids and using the colorblind-friendly palette viridis.

library(knitr)
knitr::opts_chunk$set(
    echo = TRUE,
    message = FALSE,
    warning = FALSE
)

here package

library("here")

getwd()

ggplot2

library(ggplot2)

results <- read.csv("results.csv")

#plot mean and SE of data
r <- ggplot(results, aes(timepoint, normalized_area, color = construct)) +
  labs(y = "y", x = "x") +  ggtitle("") +
  guides(colour = guide_legend(title = "")) +
  stat_summary(fun = mean, geom = "line") +
  stat_summary(fun.data = mean_se, geom = "pointrange", size = 0.25)
r

#lay out panels in a grid
s <- r + facet_grid(cols = vars(rapa))
s

viridis package

Colorblind-friendly color maps

More info here

library(viridis)

#viridis uses sequential but ggplot2 needs discrete color maps (use paletteer)
palette <- paletteer::paletteer_c("viridis::viridis", 5)

t <- s + scale_color_manual(values = palette)
t

ggstatsplot package

Extension of ggplot2 package with details from statistical tests

Much more to explore here

library(ggstatsplot)

#filter data
results_filtered <- dplyr::filter(results, timepoint == 30, rapa == "w")

#ggstatsplot
#for reproducibility
set.seed(123)

#plot filtered data
p <- ggbetweenstats(
  data = results_filtered,
  x = construct, ## grouping/independent variable
  y = normalized_area, ## dependent variables
  grouping.var = construct,
  type = "np", ## type of statistics
  xlab = "", ## label for the x-axis
  ylab = "y", ## label for the y-axis
  plot.type = "violin", ## type of plot
  outlier.tagging = F, ## whether outliers should be flagged
  outlier.coef = 1.5, ## coefficient for Tukey's rule
  ggtheme = ggplot2::theme_bw(), ## a different theme
  package = "ggsci", ## package from which color palette is to be taken
  palette = "uniform_startrek", ## choosing a different color palette
  title = ""
)
p

#viridis uses sequential but ggstatsplot needs discrete color maps (use paletteer)
n <- p + scale_color_manual(values = palette)
n